package com.dede.screensaverhelper.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.os.Build
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.MenuItem
import com.dede.screensaverhelper.*
import com.dede.screensaverhelper.util.JavaShellUtil
import org.jetbrains.anko.doAsync
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFile()

        val hasPer = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SECURE_SETTINGS)
        if (hasPer != PackageManager.PERMISSION_GRANTED) {
            requestPermission()
        }

//        startService<DreamServer>()

        setShortcut()
    }

    private fun setShortcut() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutInfos = ArrayList<ShortcutInfo>()
            val intent = Intent(this, DreamTempActivity::class.java)
            intent.action = Intent.ACTION_VIEW
            val label = resources.getString(R.string.dream_now)
            val info = ShortcutInfo.Builder(this, label)
                    .setShortLabel(label)
                    .setLongLabel(label)
//                    .setIcon(Icon.createWithResource(this, R.drawable.ic_tile_dream_now))
                    .setIntent(intent)
                    .build()
            shortcutInfos.add(info)
            val shortcutManager = this.getSystemService(ShortcutManager::class.java)
            shortcutManager.dynamicShortcuts = shortcutInfos
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun requestPermission() {
        doAsync {
            JavaShellUtil.execCommand(
                    "pm grant ${this@MainActivity.packageName} ${Manifest.permission.WRITE_SECURE_SETTINGS}")
        }
    }

    private fun initFile() {
        val file = dexFile()
        if (!BuildConfig.DEBUG && file.legalFile()) {
            return
        }

        doAsync {
            var input: InputStream? = null
            var output: FileOutputStream? = null
            try {
                input = assets.open(Config.dexFileName)
                output = FileOutputStream(file)
                val bytes = ByteArray(4096)
                var len = input.read(bytes)
                while (len != -1) {
                    output.write(bytes, 0, len)
                    len = input.read(bytes)
                }
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                try {
                    input?.close()
                    output?.close()
                } catch (e: IOException) {
                }
            }
        }
    }
}
